package com.flyqiu.flow.bpmn.element;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.flow.bpmn.FlyFlowElementConstants;
import com.flyqiu.flow.bpmn.base.ElementCandidateConfig;
import com.flyqiu.flow.bpmn.base.IMultiInstance;
import com.flyqiu.flow.bpmn.base.UserTaskMultiInstance;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class UserTask extends ProcNodeModel {


    private List<ElementCandidateConfig> candidateConfigs;

    private IMultiInstance multiInstance;

    @Override
    public String getElementType() {
        return FlyFlowElementConstants.ELEMENT_TYPE_USER_TASK;
    }

    @Override
    protected void initElement(JSONObject element, ProcessModel processModel) {

        initCandidateConfigs(element);
        initMultiInstance(element);
    }

    protected void initCandidateConfigs(JSONObject element) {
        JSONArray candidateConfig = element.getJSONArray("candidateConfigs");
        if (candidateConfig == null || candidateConfig.isEmpty()) {
            return;
        }
        candidateConfigs = new ArrayList<>();
        int size = candidateConfig.size();
        int index = 0;
        while (index < size) {
            JSONObject jsonObject = candidateConfig.getJSONObject(index);
            candidateConfigs.add(new ElementCandidateConfig(jsonObject));
            index = index + 1;
        }


    }

    protected void initMultiInstance(JSONObject element) {
        multiInstance = UserTaskMultiInstance.create(element);
    }


}
